<?php

/**
 * This class serves to create HTML elements.
 *  
 * @package Library
 * @subpackage HTML 
 */
class Html_Form
{
    /**
     * isSelected
     * Checks if a given element is selected.
     *
     * @param mixed $selected selected elements
     * @param mixed $k tested element
     * @return boolean result
     */
    static function isSelected($selected, $k)
    {
        if (! is_array($selected))
            $selected = array($selected);
        return (in_array($k, $selected) ? 'selected' : '');
    }

    /**
     * makeOptions
     * Creates html options.
     *
     * @param array $vals hash of key=>value
     * @param mixed $selected selected element(s)
     * @param string $all name of first empty option, usually 'All'
     * @return string
     */
    static function makeOptions($vals, $selected, $all='', $value_is_title, $is=false)
    {
        $html = '';
        if ($all)
            $html .= '<option value="">' . $all . '</option>';
        foreach ($vals as $k => $label){
            if ($value_is_title)
                $html .= '<option ' . Html_Form::isSelected($selected, $k) . ' value="' . $label . '">' . $label . '</option>';
            elseif($is)
                $html .= '<option ' . Html_Form::isSelected($selected, $label) . ' value="' . $label . '">' . $label . '</option>';
            else
                $html .= '<option ' . Html_Form::isSelected($selected, $k) . ' value="' . $k . '">' . $label . '</option>';    
        }
        return $html;
    }

 
    /**
     * isChecked
     * Checks if given element is checked.
     *
     * @param mixed $selected
     * @param string $k
     * @return string
     */
    static function isChecked($selected, $k)
    {
        if (! is_array($selected))
            $selected = array($selected);
        return (in_array($k, $selected) ? 'checked' : '');
    }

    /**
     * makeRadios
     * Creates html radiobuttons.
     *
     * @param string $name
     * @param array $vals
     * @param string $selected
     * @return string
     */
    static function makeRadios($name, $vals, $selected)
    {
        $html = '';
        foreach ($vals as $k => $label)
            $html .= '<input type="radio" name="'.$name.'" value="' . $k . '" ' . Html_Form::isChecked($selected, $k) . '>'.$label;
        return $html;
    }
}